package com.ethan.springcloud.helloserviceclient;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * @author daifei
 * @date 2019/7/10
 */
@Service
@Slf4j
public class HelloServiceWithFallback {

    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "fallback")
    public String hello(String name) {
        String responseEntity = restTemplate.getForObject("http://HELLOSERVICE/hello?name={1}", String.class, name);
        log.info("hello service :{}", responseEntity);
        return responseEntity;
    }

    /**
     * 错误回调方法参数签名必须与原方法一致
     *
     * @param name
     * @return
     */
    public String fallback(String name, Throwable e) {
        log.info("这是造成服务降级的异常信息：{}",name, e);
        return "this is fallback message! ";
    }

}
